#ifndef AZXMLDATASOURCE_H
#define AZXMLDATASOURCE_H

#include <QXmlStreamReader>
#include <QFile>
#include <QPointer>

#include "Az.h"
#include "AzAbstractDataSource.h"

class AZLIB_EXPORT AzXmlDataSourceException: public AzDataSourceException {
public:
    enum XmlError {
        NoDeviceSpecifyError,
        OpenFileError,
        ExternalDeviceMustbeOpenError,
        XmlExcpectTagError,
        XmlExpectAttrError
    };
    AzXmlDataSourceException(XmlError,const QString& = QString(),const QString& = QString());
    AzXmlDataSourceException(AzDataSourceException::Error);

    static QString nativeCodeToMessage(XmlError);
    static AzDataSourceException::Error nativeCodeToCode(XmlError);
};


class AZLIB_EXPORT AzXmlDataSource: public AzAbstractDataSource {
Q_OBJECT
public:
    AzXmlDataSource(QObject* = 0);
    ~AzXmlDataSource();

    bool isConnectReadOnly() const;

    void setDevice(QIODevice*);
    inline const QIODevice *device() const                      { return mDevice;           }

protected:
    void internalOpen();
    void internalClose();
    virtual void estabsilshConnection();

    virtual void writeMetadata(const AzFieldList&);
    virtual void writeRecord(const AzRecord*);

    virtual AzFieldList readMetadata();
    virtual int readRecords(AzRecord*, const AzRecord*,int);

    void doBeforeRead();

    void doBeforeWrite();
    void doAfterWrite();

    void doBeforeWriteChildRecords(const AzRecord*);
    void doAfterWriteChildRecords(const AzRecord*);

private:
    void writeRecord(const AzRecordValues&,const char *tag);

    AzField readField();
    AzRecordData readRecord();
    AzRecordValues readRow();


    QPointer<QIODevice> mDevice;
    QXmlStreamReader mXmlStreamReader;
    QXmlStreamWriter mXmlStreamWriter;
};
#endif //AZXMLDATASOURCE_H
